package com.lv.common.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.lv.common.R;


/**
 * 作者： ygx
 * 创建日期：2022/5/18 9:49 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * _              _           _     _   ____  _             _ _
 * / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
public abstract class BaseDialog extends Dialog {

    private boolean iscancelable;//点击外部是否可以dismiss
    private boolean isBackCanCelable;//返回键是否可以取消dialog
    private Context mContext;
    private int gravity = -1;
    private boolean widthIsMatch;
    private boolean heightIsMatch;
    public BaseDialog(Context mContext, boolean isCancelable, boolean isBackCancelable) {
        super(mContext, R.style.MySimCenterDialog);//这里设置dialog的统一主题，全屏、背景透明等
        this.mContext = mContext;
        this.iscancelable = isCancelable;
        this.isBackCanCelable = isBackCancelable;

        widthIsMatch = true;
        heightIsMatch = false;
    }

    public BaseDialog(Context mContext, boolean isCancelable, boolean isBackCancelable, int gravity, boolean widthIsMatch, boolean heightIsMatch) {
        super(mContext, R.style.MySimCenterDialog);//这里设置dialog的统一主题，全屏、背景透明等
        this.mContext = mContext;
        this.iscancelable = isCancelable;
        this.isBackCanCelable = isBackCancelable;
        this.gravity = gravity;
        this.widthIsMatch = widthIsMatch;
        this.heightIsMatch = heightIsMatch;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());//这行一定要写在前面
        setCancelable(iscancelable);//点击外部不可dismiss
        setCanceledOnTouchOutside(isBackCanCelable);
        Window window = this.getWindow();
        if (gravity == -1) {

            window.setGravity(Gravity.CENTER);//设置弹框对其方式
        } else {
            window.setGravity(gravity);//设置弹框对其方式
        }
        WindowManager.LayoutParams params = window.getAttributes();
        if (widthIsMatch) {
            params.width = WindowManager.LayoutParams.MATCH_PARENT;//设置宽高模式，
        } else {
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;//设置宽高模式，
        }
        if (heightIsMatch) {
            params.height = WindowManager.LayoutParams.MATCH_PARENT;//设置宽高模式，
        } else {
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;//设置宽高模式，
        }
        window.setAttributes(params);
        initView();
    }

    protected abstract void initView();

    protected abstract int getLayoutId();

    private void fullScreenImmersive(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(uiOptions);
        }
    }

    //-------------------------隐藏系统导航栏-----------------------------------
    @Override
    public void show() {
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        super.show();
        fullScreenImmersive(getWindow().getDecorView());
        this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    }
}


